package cn.xfdzx.android.apps.shop.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.MesCenterListBean;
import cn.xfdzx.android.apps.shop.contract.MessageCenterContract;
import cn.xfdzx.android.apps.shop.presenter.AfterServicePresenter;
import cn.xfdzx.android.apps.shop.presenter.MessageTypePresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import cn.xfdzx.android.apps.shop.widget.CornerImageView;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

/*
/* 消息中心页面
 */
public class MessageCenterActivity extends BaseMvpActivity<MessageTypePresenter>implements MessageCenterContract.View {

    @BindView(R.id.iv_back)
    ImageView ivBack;      //标题栏-左边按钮
    @BindView(R.id.img_type)
    CornerImageView img_type;
    @BindView(R.id.img_type_order)
    CornerImageView img_type_order;
    @BindView(R.id.img_type_customer)
    CornerImageView img_type_customer;
    @BindView(R.id.img_type_tixing)
    CornerImageView img_type_tixing;

    @BindView(R.id.tv_mes_content)
    TextView tv_mes_content;
    @BindView(R.id.tv_num)
    TextView tv_num;

    @BindView(R.id.tv_mes_content_order)
    TextView tv_mes_content_order;
    @BindView(R.id.tv_num_order)
    TextView tv_num_order;

    @BindView(R.id.tv_mes_content_customer)
    TextView tv_mes_content_customer;
    @BindView(R.id.tv_num_customer)
    TextView tv_num_customer;

    @BindView(R.id.tv_mes_content_tixing)
    TextView tv_mes_content_tixing;
    @BindView(R.id.tv_num_tixing)
    TextView tv_num_tixing;
    private MesCenterListBean.DataBean.CouponBean coupon;
    private MesCenterListBean.DataBean.OrderBean order;
    private MesCenterListBean.DataBean.CustomerServiceBean customerService;
    private MesCenterListBean.DataBean.RemindBean remind;


    @Override
    public int getLayoutId() {
        return R.layout.activity_messagecenter;
    }

    @Override
    public void initView() {
        mPresenter = new MessageTypePresenter();
        mPresenter.attachView(this);

        mPresenter.getMesTypeList();

        img_type.setType(CornerImageView.TYPE_ROUND);
        img_type_order.setType(CornerImageView.TYPE_ROUND);
        img_type_customer.setType(CornerImageView.TYPE_ROUND);
        img_type_tixing.setType(CornerImageView.TYPE_ROUND);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void getMesTypeList(MesCenterListBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            coupon = bean.getData().getCoupon();
            order = bean.getData().getOrder();
            customerService = bean.getData().getCustomerService();
            remind = bean.getData().getRemind();

            if (EmptyUtil.isNotEmpty(coupon)){
                if (EmptyUtil.isNotEmpty(coupon.getContent())){
                    tv_mes_content.setText(coupon.getContent());
                }
                if (coupon.getIs_show() == 0){//未读
                    tv_num.setVisibility(View.VISIBLE);
                    if (coupon.getCount()<99){
                        tv_num.setText(coupon.getCount()+"");
                    }else {
                        tv_num.setText("99+");
                    }

                }else {
                    tv_num.setVisibility(View.GONE);
                }
            }

            if (EmptyUtil.isNotEmpty(order)){
                if (EmptyUtil.isNotEmpty(order.getContent())){
                    tv_mes_content_order.setText(order.getContent());
                }
                if (order.getIs_show() == 0){//未读
                    tv_num_order.setVisibility(View.VISIBLE);
                    if (coupon.getCount()<99){
                        tv_num_order.setText(order.getCount()+"");
                    }else {
                        tv_num_order.setText("99+");
                    }
                }else {
                    tv_num_order.setVisibility(View.GONE);
                }
            }

            if (EmptyUtil.isNotEmpty(customerService)){
                if (EmptyUtil.isNotEmpty(customerService.getContent())){
                    tv_mes_content_customer.setText(customerService.getContent());
                }
                if (customerService.getIs_show() == 0){//未读
                    tv_num_customer.setVisibility(View.VISIBLE);
                    if (coupon.getCount()<99){
                        tv_num_customer.setText(customerService.getCount()+"");
                    }else {
                        tv_num_customer.setText("99+");
                    }
                }else {
                    tv_num_customer.setVisibility(View.GONE);
                }
            }

            if (EmptyUtil.isNotEmpty(remind)){
                if (EmptyUtil.isNotEmpty(remind.getContent())){
                    tv_mes_content_tixing.setText(remind.getContent());
                }
                if (remind.getIs_show() == 0){//未读
                    tv_num_tixing.setVisibility(View.VISIBLE);
                    if (coupon.getCount()<99){
                        tv_num_tixing.setText(remind.getCount()+"");
                    }else {
                        tv_num_tixing.setText("99+");
                    }
                }else {
                    tv_num_tixing.setVisibility(View.GONE);
                }
            }

        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(this, "login").remove("head_url");
                UtilsData.getPreference(this, "login").remove("nickname");
                UtilsData.getPreference(this, "login").remove("is_store");
                UtilsData.getPreference(this, "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(this, OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:    //返回按钮点击
                finish();
                break;

        }
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }

}
